package com.Lishuai.Servce.Imp;

import com.Lishuai.Mapper.BookMapper;
import com.Lishuai.Mapper.MangermentMapper;
import com.Lishuai.Mapper.UserMapper;
import com.Lishuai.Pojo.Book;
import com.Lishuai.Pojo.User;
import com.Lishuai.Servce.UserServce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@Service
public class UserServceImp implements UserServce {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private MangermentMapper mangermentMapper;
     @Override
    public boolean login(User user) {
        User login = userMapper.login(user);
        boolean equals = user.getPassword().equals(login.getPassword());
        return equals;
    }

    @Override
    public int addUser(User user) {

         return userMapper.addUser(user);
    }

    @Override
    public String changeHeadImg(int id, MultipartFile[] filePic) throws Exception{
        for (MultipartFile multipartFile : filePic) {
            // 获取文件名
            String originalFilename = multipartFile.getOriginalFilename();
            //获取当前时间
            String time = System.currentTimeMillis() + "";
            originalFilename = time + originalFilename;
            // 保存到服务器
            multipartFile.transferTo(new File("D:/vuecli/book_store/src/assets/img/" + originalFilename));
            userMapper.changeHeadImg(id,originalFilename);
        }
        return "success";
    }
    @Override
    public List<Book> sekect(Book book) {
        List<Book> sekect = bookMapper.sekect(book);
        return sekect;
    }

    @Override
    public List<Book> findAll() {
        List<Book> all = bookMapper.findAll();
        return all;
    }
    @Transactional
    @Override
    public Integer buyBook(User user, Book book) {
        User login = userMapper.login(user);
        if (login.getMoney() >= book.getPrice()){
            userMapper.buyBook(user.getId(),book.getPrice());
            bookMapper.updateNumber(book.getId());
            mangermentMapper.soldBook(book.getPrice());
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public String getImage(int id) {
        String image = userMapper.getImage(id);
        String img = "@/assets/img/" + image;
        return img;

    }

    @Override
    public void recharge(int id, double money) {
        userMapper.recharge(id,money);
    }

    @Override
    public PageInfo<Book> getBookPage(int pages) {
        PageHelper.startPage(pages, 2);
        List<Book> all = bookMapper.findAll();
        PageInfo<Book> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }
}
